/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Assignment;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;

/**
 *
 * @author ryuju
 */
@Singleton
@LocalBean
public class AssignmentManager {
    @EJB
    private AssignmentFacade assignmentFacade;
     
    public List<Assignment> getAssignments() {
        //return assignmentFacade.findAssignmentByStartEndDate();
        return assignmentFacade.findAll();
    }
    
    public boolean createAssignment(Assignment assignment) {
        if(null == assignment)
            return false;
        
        //assignment.setStartDate(new Date(System.currentTimeMillis()));
        
        assignmentFacade.create(assignment);
        return true;
    }
    
    public boolean modifyAssignment(Assignment assignment) {
        if(null == assignment)
            return false;
        
        Assignment target = assignmentFacade.find(assignment.getId());
        if(null == target)
            return false;
        
        target.setContent(assignment);
        assignmentFacade.edit(target);
        
        return true;
    }
    
    public List<Assignment> getAllAssignments() {
        return assignmentFacade.findAll();
    }
    
    public Assignment getAssignment(Long asmtID)
    {
        if(null == asmtID)
            return null;
        
        Assignment asmt = null;
        try {
            asmt = assignmentFacade.findAssignmentByID(asmtID);
        } catch (Exception e) {
            asmt = null;
        }
        return asmt;
    }
    
}
